import { FieldRule } from '@arco-design/web-vue';

interface LengthRange {
  min?: number;
  max?: number;
  message?: string;
}

export const useValidator = () => {
  // eslint-disable-next-line no-shadow
  const required = (required = true, message?: string): FieldRule => {
    return {
      required,
      message: message || '该项为必填项',
    };
  };

  const maxLength = (options: LengthRange): FieldRule => {
    const { max, message } = options;
    return {
      max,
      message: message || `最长为 ${max} 个字符`,
    };
  };

  const lengthRange = (options: LengthRange): FieldRule => {
    const { min, max, message } = options;

    return {
      min,
      max,
      message: message || `长度在 ${min} 到 ${max} 个字符`,
    };
  };

  const notSpace = (message?: string): FieldRule => {
    return {
      validator: (val: string, callback: (error?: string) => void) => {
        if (val?.indexOf(' ') !== -1) {
          callback(message || '不能包含空格');
        } else {
          callback();
        }
      },
    };
  };

  const notSpecialCharacters = (message?: string): FieldRule => {
    return {
      validator: (val: string, callback: (error?: string) => void) => {
        if (/[`~!@#$%^&*()_+<>?:"{},./;'[\]]/gi.test(val)) {
          callback(message || '不能包含特殊字符');
        } else {
          callback();
        }
      },
    };
  };

  const checkUsername = (message?: string): FieldRule => {
    return {
      validator: (val: string, callback: (error?: string) => void) => {
        const reg = /^[A-Za-z][A-Za-z1-9_-]+$/;
        if (val && !reg.test(val)) {
          callback(message || '用户名格式不正确');
        } else {
          callback();
        }
      },
    };
  };

  const checkEmail = (message?: string): FieldRule => {
    return {
      validator: (val: string, callback: (error?: string) => void) => {
        const mailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
        if (val && !mailReg.test(val)) {
          callback(message || '邮箱格式不正确');
        } else {
          callback();
        }
      },
    };
  };

  const limitLetter = (message?: string): FieldRule => {
    return {
      validator: (val: string, callback: (error?: string) => void) => {
        const reg = /^\w+$/;
        if (val && !reg.test(val)) {
          callback(message || '请输入字母/数字/下划线');
        } else {
          callback();
        }
      },
    };
  };

  const checkMobile = (message?: string): FieldRule => {
    return {
      validator: (val: string, callback: (error?: string) => void) => {
        const phoneReg = /^1(3|4|5|6|7|8|9)\d{9}$/;
        if (val && !phoneReg.test(val)) {
          callback(message || '手机号码格式不正确');
        } else {
          callback();
        }
      },
    };
  };

  // arco.design 的input没有format，无法使用下面方法，改为验证方式
  const limitWord = (message: string) => {
    return message.replace(/[^\w_]/g, '');
  };

  const limitDigit = (message?: string) => {
    return {
      validator: (val: string, callback: (error?: string) => void) => {
        const regExp = /^[0-9]*$/g;
        if (val && !regExp.test(val)) {
          callback(message || '请输入数字');
        } else {
          callback();
        }
      },
    };
  };

  const limitLetterDigit = (message?: string): FieldRule => {
    return {
      validator: (val: string, callback: (error?: string) => void) => {
        const regExp = /^[0-9a-zA-Z]+$/g;
        if (val && !regExp.test(val)) {
          callback(message || '请输入字母/数字');
        } else {
          callback();
        }
      },
    };
  };

  const limitCard = (message?: string) => {
    return {
      validator: (val: string, callback: (error?: string) => void) => {
        const regExp = /^[0-9a-zA-Z]+$/g;
        if (val && (val.length != 18 || !regExp.test(val))) {
          callback(message || '请输入正确的身份证号');
        } else {
          callback();
        }
      },
    };
  };
  // 密码规则
  const checkPassword = () => {
    return {
      validator: (val: string, callback: (error?: string) => void) => {
        const regExp = /^[a-zA-Z\d]{6,18}$/g;
        if (!regExp.test(val)) {
          callback('请输入正确规则的密码');
        } else {
          callback();
        }
      },
    };
  };

  return {
    required,
    maxLength,
    lengthRange,
    notSpace,
    notSpecialCharacters,
    checkUsername,
    checkEmail,
    checkMobile,
    limitLetter,
    limitWord,
    limitCard,
    limitDigit,
    limitLetterDigit,
    checkPassword,
  };
};
